package com.hzlh.common.util;

import java.awt.Image;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.web.multipart.MultipartFile;

public class CheckImage {
	private static Logger logger = Logger.getLogger(CheckImage.class);
	/**  
     * 验证图片大小 限制在一M内  
     * @param file  
     * @return  
     */ 
	@SuppressWarnings("serial")
	public static Map<String,String> imgMap=new HashMap<String,String>(){
		{
			put("image/jpeg",".jpg");
			put("image/pjpeg",".jpg");
			put("image/x-png",".png");
			put("image/png",".png");
		}
	};
	
    @SuppressWarnings("resource")
	public static HashMap<String, Object> checkSize(File file){   
        HashMap<String, Object> map=new HashMap<String, Object>();   
        FileInputStream ins;   
        try{   
            ins = new FileInputStream(file);   
            //判断文件是否超过一M   
            if (ins.available() > 1024 * 1024 *1) {   
                file.delete();   
                map.put("success", false);   
                map.put("msg", "上传文件不得超过1M");   
            }else{   
                map.put("success", true);   
                map.put("msg", "");   
            }   
        }catch (FileNotFoundException e){   
            map.put("success", false);   
            map.put("msg", "系统读取图片时出现异常，请联系管理员");   
        }catch (IOException e){   
               
            map.put("success", false);   
            map.put("msg", "系统读取图片时出现异常，请联系管理员");   
        }   
        return map;   
    }
    
    /**
     * @Description: 验证图片大小
     * @param file
     * @param size
     * @return boolean  
     * @throws
     * @author liu_wenfeng
     * @date 2015年8月12日 下午1:42:07
     */
	public static boolean validImageSize(MultipartFile file , int size){
		InputStream ins = null;
        boolean result = false;
        try{   
        	ins = file.getInputStream();   
            //判断文件是否超过 size 单位：kb  
            if (!(ins.available() > size)) {   
            	result = true;
            }   
        }catch (FileNotFoundException e){
        	logger.info("找不到读取文件");
        }catch (IOException e){   
        	logger.info("系统读取图片时出现异常，请联系管理员");
        }finally{
        	try {
				ins.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
        }
        return result;   
    }
	
	/**
	 * @Description: 验证图片尺寸大小
	 * @param file
	 * @param width
	 * @param height
	 * @return boolean  
	 * @throws
	 * @author liu_wenfeng
	 * @date 2015年8月12日 下午1:42:31
	 */
	public static boolean validImageSize(MultipartFile file ,int width ,int height){
        boolean result = false;
        Image src;   
        try{   
            src = javax.imageio.ImageIO.read(file.getInputStream());   
            int w = src.getWidth(null);     
            int h = src.getHeight(null);   
            if(w <= width && h <= height){   
                result = true;
            }  
        }catch (IOException e){   
            e.printStackTrace();   
            logger.info("系统读取图片大小出现异常，请联系管理或调整图片");   
        }
        return result;   
    }
    
    /**  
     * 检测文件长度是否符合  
     * @param height  
     * @return  
     */  
    public static HashMap<String, Object> checkImgSize(File file,int width,int height){   
        HashMap<String, Object> map=new HashMap<String, Object>();   
        Image src;   
        try{   
            src = javax.imageio.ImageIO.read(file);   
            int w = src.getWidth(null);     
            int h = src.getHeight(null);   
            if(w<=width && h<=height){   
                map.put("success", true);   
                map.put("msg", "true");   
            }else{   
                map.put("success", false);   
                map.put("msg", "文件上传失败，请上传图片宽："+width+" 高:"+height);   
            }   
        }catch (IOException e){   
             
            e.printStackTrace();   
            map.put("success", false);   
            map.put("msg", "系统读取图片大小出现异常，请联系管理或调整图片");   
        }   
        return map;   
    }
    
    
    /**
     * 检测LOGO是否符合
     * @param file
     * @param width
     * @param height
     * @return
     */
    public static HashMap<String, Object> checkImgSizeLoGo(File file,int width,int height){   
        HashMap<String, Object> map=new HashMap<String, Object>();   
        Image src;   
        try{   
            src = javax.imageio.ImageIO.read(file);   
            int w = src.getWidth(null);     
            int h = src.getHeight(null);   
            if(w==width && h==height){   
                map.put("success", true);   
                map.put("msg", "true");   
            }else{   
                map.put("success", false);   
                map.put("msg", "文件上传失败，请上传图片宽："+width+" 高:"+height);   
            }   
        }catch (IOException e){   
             
            e.printStackTrace();   
            map.put("success", false);   
            map.put("msg", "系统读取图片大小出现异常，请联系管理或调整图片");   
        }   
        return map;   
    }

	public static int getImageHeight(MultipartFile file){
		Image src;
		int h = 0;
            try {
				src = javax.imageio.ImageIO.read(file.getInputStream());
				h = src.getHeight(null);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
            return h;
            
	}
	public static int getImageWidth(MultipartFile file){
		Image src;
		int h = 0;
            try {
				src = javax.imageio.ImageIO.read(file.getInputStream());
				h = src.getWidth(null);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
            return h;
            
	}
}
